package com.aiti.lulian.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.gxmh.dto.GxmhUserRegisterDto;
import com.aiti.lulian.gxmh.dto.UserEnterpriseParamDto;
import com.aiti.lulian.gxmh.service.impl.GxmhUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-06-19 16:08
 **/
@RestController
@RequestMapping("/gxmh/userAndEnterprise")
@Slf4j
@Api(value = "工信门户--用户和入驻企业")
public class GxmhUserAndEnterpriseController {

    @Autowired
    private GxmhUserService gxmhUserService;

    @GetMapping("/userExist")
    @ApiOperation(value = "查询用户是否已经存在")
    public Message queryUserRegisterCount(@RequestParam("username") String username) {
        try {
            return Message.success(gxmhUserService.queryUserRegisterCount(username));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户注册数量异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @GetMapping("/userEnterpriseExist")
    @ApiOperation(value = "查询用户是否已经绑定企业")
    public Message queryUserEnterpriseCount(@RequestParam(value = "username", required = false) String username,
                                            @RequestParam(value = "userId", required = false) String userId) {
        try {
            return Message.success(gxmhUserService.queryUserEnterpriseCount(username, userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户注册数量异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @PostMapping("/registerUserInfo")
    @ApiOperation(value = "完善个人信息")
    public Message insertUserRegisterInfo(@RequestBody GxmhUserRegisterDto dto) {
        try {
            gxmhUserService.insertUserRegisterInfo(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("用户注册异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

    @PostMapping("/registerEnterpriseInfo")
    @ApiOperation(value = "企业入住")
    public Message registerEnterpriseInfo(@RequestBody UserEnterpriseParamDto dto) {
        try {
            gxmhUserService.registerEnterpriseInfo(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("用户注册异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常");
        }
    }

}
